﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic.OnlineSupports;
using Module.Core.Common;
using Module.Core.EntitiesInfo.TablesInfo;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.OnlineSupports
{
    public partial class OnlineSupports : AdminPage
    {
        #region Khai báo
        private int onlineSupportId = 0;
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                onlineSupportId = ObjConvertUtility.ToInt32(Request.QueryString["onlineSupportId"]);
                if (!Page.IsPostBack)
                {
                    LoadData();
                    txtDescription.Height = 180;
                    txtDescription.Width = 460;
                    //txtDescription.SetSecurityFilesGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                    //txtDescription.SetSecurityFlashGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                    //txtDescription.SetSecurityGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                    //txtDescription.SetSecurityImageGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                    //txtDescription.SetSecurityMediaGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                    //txtDescription.SetSecurityTemplateGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                }
            }
            catch (Exception ex)
            {
            }
        }

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (onlineSupportId > 0)
                {
                    btnUpdate.Visible = true;
                    btnAdd.Visible = false;
                    OnlineSupportsInfo info = OnlineSupportsBL.Select(onlineSupportId);
                    if (info != null)
                    {
                        txtTitle.Text = info.Title;
                        txtDescription.Text = info.Description;
                        txtYahoo.Text = info.Yahoo;
                        txtSky.Text = info.Sky;
                        txtEmail.Text = info.Email;
                        dropSupportType.Text = info.SupportType.ToString();
                        txtNickName.Text = info.Nickname;
                        txtPhone.Text = info.Phone;
                        txtMobile.Text = info.Mobile;
                        dropPriority.SelectedValue = info.Priority.ToString();
                    }
                }
                else
                {
                    btnUpdate.Visible = false;
                    btnAdd.Visible = true;
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region FillData
        void FillData(OnlineSupportsInfo info)
        {
            info.Title = txtTitle.Text;
            info.Mobile = txtMobile.Text;
            info.Description = txtDescription.Text;
            info.Yahoo = txtYahoo.Text;
            info.Sky = txtSky.Text;
            //info.OnlineSupportId = onlineSupportId;
            info.SupportType = int.Parse(dropSupportType.SelectedValue);
            info.Nickname = txtNickName.Text;
            info.Phone = txtPhone.Text;
            info.Email = txtEmail.Text;
            info.Priority = int.Parse(dropPriority.SelectedValue);
            //info.Status = 1;
            info.Status = Const.show;
            info.OnlineSupportId = onlineSupportId;
        }
        #endregion




        #region ClearText
        void ClearText()
        {
            txtTitle.Text = "";
            txtMobile.Text = "";
            txtDescription.Text = "";
            txtEmail.Text = "";
            txtYahoo.Text = "";
            txtSky.Text = "";
            //txtSupportType.Text = "";
            txtNickName.Text = "";
            txtPhone.Text = "";
            //txtStatus.Text = "";
        }
        #endregion

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                OnlineSupportsInfo info = new OnlineSupportsInfo();
                FillData(info);
                OnlineSupportsBL.Update(info);
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                OnlineSupportsInfo info = new OnlineSupportsInfo();
                FillData(info);
                int i = OnlineSupportsBL.Insert(info);
                if (i > 0)
                {
                    lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
                    ClearText();
                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }


    }
}